<?php
/**
 * Created by Abner
 * User: hc01
 * Date: 2017-08-19
 * Time: 15:58
 */

namespace app\admin\model;


class Trade extends BaseModel
{
    public function getStatusNameAttr($value,$data)
    {
        $status = [0=>'未结算',1=>'已结算'];
        return $status[$data['status']];
    }
    public function admin()
    {
        return $this->belongsTo('Admin','admin_id');
    }
    
    public static function getTradeBillSum($agentName='',$status=0,$agent_id='',$level=0)
    {
        
        $pay_sum = db('trade_bill')->where("trade_status",'交易成功')->where('agent_name','like','%'.$agentName. '%')->where('status',$status)
        ->group('trade_id')->where("pay_gateway",'银联养卡（至平台）')->field("sum(trade_money) as pay_sum,trade_id,status,agent_name")->select()->toArray();
         
         $sum = db('trade_bill')->where("trade_status",'交易成功')->where('agent_name','like','%'.$agentName. '%')->where('status',$status)
        ->group('trade_id')->field("sum(trade_money) as sum,trade_id,status,agent_name")->select()->toArray();
        // $lista = array_merge($get_sum,$pay_sum);
      
        $nextAgent = getNextAgent($agent_id,$level);
        
        if($nextAgent && array_key_exists('children',$nextAgent)){
            foreach ($nextAgent['children'] as $k => $v) {
                $child_sum = db('trade_bill')->where("trade_status",'交易成功')->where('agent_name','like','%'.$nextAgent['children'][$k]['chnlName']. '%')->where('status',$status)
                ->group('trade_id')->field("sum(trade_money) as get_sum,trade_id,status,agent_name")->select()->toArray();
                 $childpay_sum = db('trade_bill')->where("trade_status",'交易成功')->where('agent_name','like','%'.$nextAgent['children'][$k]['chnlName']. '%')->where('status',$status)
                ->group('trade_id')->where("pay_gateway",'银联养卡（至平台）')->field("sum(trade_money) as pay_sum,trade_id,status,agent_name")->select()->toArray();
       
            }
            $sum = array_merge_recursive($sum,$child_sum);
            $pay_sum = array_merge_recursive($pay_sum,$childpay_sum);
        }
        if($sum){
            foreach($sum as $k=>$v){
                if($pay_sum && $pay_sum[$k]['trade_id'] == $sum[$k]['trade_id']){
                    $sum[$k]['pay_sum'] = $pay_sum[$k]['pay_sum'];
                }else{
                    $sum[$k]['pay_sum'] = 0;
                }
                $sum[$k]['status_name'] = $sum[$k]['status'] == 0 ? '未结算' : '已结算';
                $sum[$k]['get_sum'] = $sum[$k]['sum'] - $sum[$k]['pay_sum'];
                $sum[$k]['time'] = self::get($sum[$k]['trade_id'])['time'];
                $sum[$k]['chnlName']  = substr($sum[$k]['agent_name'],14);
                $sum[$k]['chnlId'] = substr($sum[$k]['agent_name'],1,12);
            }
        }
        
       
        // $list = array_map(function($Data,$arr){
        //     return [ 
        //         'get_sum' => $arr['get_sum'],
        //         'pay_sum' => $Data['pay_sum'],
        //         'trade_id' => ($Data['trade_id']??$arr['trade_id']),
        //         'status_name' =>$arr['status'] == 0 ? '未结算' : '已结算',
        //         'agent_name'=>($Data['agent_name']??$arr['agent_name'])
        //     ];
        // }, $pay_sum,$get_sum);
         
        // foreach ($list as $k=>$v){
        //     $list[$k]['time'] = self::get($list[$k]['trade_id'])['time'];
        //     $list[$k]['sum'] = $list[$k]['get_sum'] + $list[$k]['pay_sum'];
        //     $list[$k]['chnlName']  = substr($list[$k]['agent_name'],14);
        //     $list[$k]['chnlId'] = substr($list[$k]['agent_name'],1,12);
        // }
       
        return $sum;
    }
}